<?php
class helper_url extends system_view_helper_abstract {
	protected $_index = null;
	protected $_params = array();
	protected $_query = array();
	public function url($index, $params = array(), $query = array()) {
		$this->_index = $index;
		$this->_params = $params;
		$this->_query = $query;
		return $this;
	}
	
	public function getRoute($index) {
		return system_controller_front::getInstance()->getRouter()->getRoute($index);
	}
	
	public function isCurrent() {
		return $this->assemble() == $this->view->baseUrl() . system_controller_front::getInstance()->getRequest()->getPathInfo();
	}
	public function isCurrentRoute() {
		return $this->_index == system_controller_front::getInstance()->getRouter()->getCurrentRouteIndex();
	}
	
	public function assemble() {
		$route = $this->getRoute($this->_index);
		try {
			$url = (string) $route->assemble($this->_params);
		} catch (Exception $e) {
			$url = '';
		}
		return $url . (!empty($this->_query) ? '?' . http_build_query($this->_query) : '');
	}
	
	public function __toString() {
		return $this->assemble();
	}
}
